package com.github.aloxc.plugin.restplus.listener.search;


import com.github.aloxc.plugin.restplus.common.ServiceHelper;
import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.SwaggerConfig;
import com.github.aloxc.plugin.restplus.method.HttpMethod;
import com.github.aloxc.plugin.restplus.navigation.action.RestServiceItem;
import com.github.aloxc.plugin.restplus.navigator.RestServiceProject;
import com.intellij.navigation.ChooseByNameContributor;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author leero
 */
public class SearchRequestMappingContributor implements ChooseByNameContributor {
    Module myModule;

    private List<RestServiceItem> navItem;

    public SearchRequestMappingContributor(Module myModule) {
        this.myModule = myModule;
    }

    /**
     * Returns the list of names for the specified project to which it is possible to navigate by name. 所有该类型的文件列表
     */
    @NotNull
    @Override
    public String[] getNames(Project project, boolean onlyThisModuleChecked) {
        String[] names = null;
        List<RestServiceItem> itemList;
        ///todo 查找 project 中所有符合 rest url 类型文件，包含 request 接口
        if (onlyThisModuleChecked && myModule != null) {
            itemList = ServiceHelper.buildRestServiceItemListUsingResolver(project, myModule);
        } else {
            itemList = ServiceHelper.buildRestServiceItemListUsingResolver(project);
        }
        addSwaggerList(project, itemList);
        navItem = itemList;

        if (itemList != null) {
            names = new String[itemList.size()];
        }

        for (int i = 0; i < itemList.size(); i++) {
            RestServiceItem requestMappingNavigationItem = itemList.get(i);
            names[i] = requestMappingNavigationItem.getName();
        }

        return names;
    }

    private static void addSwaggerList(Project project, List<RestServiceItem> itemList) {
        RestConfig restConfig = RestConfig.loadConfig(project);
        List<SwaggerConfig> swaggerConfigList = restConfig.getSwaggerConfigList();
        List<RestServiceProject> projects = new ArrayList<>();
        swaggerConfigList.stream().forEach(swaggerConfig -> {
            swaggerConfig.getSwaggerRequestList().stream().forEach(swaggerRequest -> {
                RestServiceItem item = new RestServiceItem(null,null,
                        swaggerRequest.getMethod() == HttpMethod.POST_BODY ? HttpMethod.POST.name() : swaggerRequest.getMethod().name(),
                        swaggerRequest.getUrl(),
                        swaggerRequest.getMethod() == HttpMethod.POST_BODY,swaggerRequest.getName());
                item.setSwagger(true);
                item.setParentName(swaggerConfig.getName());
                itemList.add(item);
            });
        });
    }
/*

    private List<RestServiceItem> findAllRestServiceItemList(Module myModule) {
//        List<RestServiceItem> itemList = ServiceHelper.buildRestServiceItemList(myModule);
        return ServiceHelper.buildRestServiceItemListUsingResolver(myModule);
    }

    private List<RestServiceItem> findAllRestServiceItemList(Project project) {
//        Arrays.stream(SpringRequestAnnotation.values()).flatMap(annotation -> )
        List<RestServiceItem> itemList = ServiceHelper.buildRestServiceItemList(project);

        return itemList;
    }
*/

    /**
     * Returns the list of navigation items matching the specified name. 匹配，对比
     *
     * @param name
     * @param pattern
     * @param project
     * @param onlyThisModuleChecked
     * @return
     */
    @NotNull
    @Override
    public NavigationItem[] getItemsByName(String name, String pattern, Project project, boolean onlyThisModuleChecked) {
        NavigationItem[] navigationItems = navItem.stream().filter(item -> item.getName().equals(name)).toArray(NavigationItem[]::new);
        return navigationItems;

    }
}
